﻿using System;
using System.Collections.Generic;
using MediaSage.Lib.Database;
using MediaSage.Lib.Plugin;

namespace MediaSage.Lib.MediaUpdate
{
    internal class MediaFilename
    {
        private Configuration.Config config;
        private Logger logger;
        private ThreadManager.ThreadManager threadManager;

        /// <summary>
        /// Class intialization, sets up our configuration, and logger instances
        /// </summary>
        public MediaFilename(Configuration.Config configuration)
        {
            this.config = configuration;
            this.logger = configuration.Logger;
            this.threadManager = configuration.ThreadManager;

            logger.Log("MediaSage.Lib.MediaUpdate.MediaFilename() initialized");
        }

        /// <summary>
        /// Returns MediaObject if the user selected a file as the right one or we were above the threshold
        /// null if none are found or user cancels
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public MediaClass GetMediaFilename(string file)
        {
            logger.Log(String.Format("Entering GetMediaFilename({0})", file));

            var pluginOrder = new Dictionary<int, PluginContainer>();
            foreach (PluginEntity pe in config.Plugins)
            {
                if (pe.IsActive && pe.PluginType == PluginType.file)
                {
                    int priority = pe.Priority;
                    if (pluginOrder.ContainsKey(priority))
                    {
                        priority++;
                        while (pluginOrder.ContainsKey(priority++))
                        {
                            priority++;
                        }
                    }
                    PluginContainer pec = new PluginContainer();
                    pec.pluginEntity = pe;
                    pluginOrder.Add(priority, pec);
                }
            }

            // We now have a list of plugins and their priority (Priority will be used to merge later)
            // This is blocking, because we are going to potentially wait for confirmation

            var activeUpdates = new List<Guid>();
            var resultList = new List<MediaSearchReturn>();

            foreach (KeyValuePair<int, PluginContainer> kvp in pluginOrder)
            {
                PluginBase plugin = config.PluginManager.GetPlugin(kvp.Value.pluginEntity);
                if (plugin != null)
                {
                    List<MediaSearchReturn> results = plugin.SearchForMediaByName(file);
                    if (results != null)
                    {
                        foreach (MediaSearchReturn msr in results)
                        {
                            resultList.Add(msr);
                        }
                    }
                }
            }

            resultList.Sort();
            resultList.Reverse();
            if (resultList.Count == 0)
            {
                // We got no matches, need to do a popup to tell the user in the MC UI Here
            }

            if (resultList[0].pctLikelyMatch >= config.GuessScoreThreshold)
            {
                logger.Log(String.Format("Exiting GetMediaFilename({0}), rc={1}", file, resultList[0].name));
                MediaClass mc = new MediaClass(config);
                mc.Title = resultList[0].name;
                mc.IMDBID = resultList[0].IMDBID;
                mc.TmdbID = resultList[0].TMDBID;
                mc.TvdbID = resultList[0].TVDBID;
                return (mc);
            }

            // At this point we have matches, but we dont have a definitive match, so we need to do a popup to show it to the user

            return null;
        }
    }
}